package cn.kgc.crowdfundingadmin2.service;

import cn.kgc.crowdfundingadmin2.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin2.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin2.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin2.pojo.Admin;
import cn.kgc.crowdfundingadmin2.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServicImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;

    @Override
    public Admin seleByid(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }


    @Override
    public Admin login(String loginAcct, String userPswd){
        Admin admin=null;
        AdminExample adminExample=new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);
        List<Admin> admins = adminMapper.selectByExample(adminExample);
        if (CrowdFundingUtils.collectionEffective(admins)){
            admin=admins.get(0);
            if (admin.getUserpswd().equals(CrowdFundingUtils.md5(userPswd))){
                //登录成功
                return admin;
            }else{
                //登陆失败
                admin=null;
            }
        }
        return admin;
    }


    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
    /*设置当前页,*/
        /*keyword没有值查询全部,有值根据账号或者名称或者邮箱地址模糊查询*/
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        PageInfo<Admin> pageInfo=null;
        if(CrowdFundingUtils.stringEffective(keyword)){

            AdminExample example=new AdminExample();
            AdminExample.Criteria criteria = example.createCriteria();
            criteria.andLoginacctLike("%"+keyword+"%");

            AdminExample.Criteria criteria2=example.createCriteria();
            criteria2.andUsernameLike("%"+keyword+"%");
            example.or(criteria2);

            AdminExample.Criteria criteria3=example.createCriteria();
            criteria3.andEmailLike("%"+keyword+"%");
            example.or(criteria3);
            List<Admin> list=adminMapper.selectByExample(example);
            pageInfo=new PageInfo<>(list);
        }else{
            List<Admin> admins = adminMapper.selectByExample(null);
            pageInfo=new PageInfo<>(admins);
        }
        /*封装pageinfo对象*/
        return pageInfo;
    }

    /*删除*/
    @Override
    public void remove(List<Integer> admins) {
    AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(admins);
        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        /*密码加密*/
        String userPaswd=admin.getUserpswd();
        userPaswd=CrowdFundingUtils.md5(admin.getUserpswd());
        admin.setUserpswd(userPaswd);
        adminMapper.insert(admin);
    }

    @Override
    public Admin seleAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        /*加密*/
        if(CrowdFundingUtils.stringEffective((admin.getUserpswd()))){
           String userPaswd=admin.getUserpswd();
        userPaswd=CrowdFundingUtils.md5(userPaswd);
        admin.setUserpswd(userPaswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
        /*更新数据库*/
    }
}
